﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Management;
using System.ComponentModel;
using NasuTek.Shion.Agent;

namespace NasuTek.Shion.GenuineAdvantage
{
    public class HardwareKey
    {
        public static string PackagedHardwareKey()
        {
            HardwareKeyData kd = new HardwareKeyData();
            kd.OS = Environment.OSVersion.ToString();
            kd.CPU = GetCPUInformation();
            kd.AgentVersion = "Shion Agent (SmartIRC4Net) Version " + typeof(IrcClient).Assembly.GetName().Version.ToString();
            return kd.ToPackagedHWKey();
        }

        public static HardwareKeyData GetHardwareKey()
        {
            HardwareKeyData kd = HardwareKeyData.GetHardwareKeyDatafromPackagedHWKey(PackagedHardwareKey());
            return kd;
        }

        static string GetCPUInformation()
        {
            string systemInformation = "";

            ObjectQuery winQuery = new ObjectQuery("SELECT * FROM Win32_Processor");

            ManagementObjectSearcher searcher = new ManagementObjectSearcher(winQuery);

            foreach (ManagementObject item in searcher.Get())
            {
                systemInformation += item["Name"];
            }

            return systemInformation;
        }
    }

    public class HardwareKeyData
    {
        string _OS, _CPU, _AgentVersion;

        [System.ComponentModel.ReadOnly(true)]
        public string AgentVersion
        {
            get { return _AgentVersion; }
            set { _AgentVersion = value; }
        }

        [System.ComponentModel.ReadOnly(true)]
        public string CPU
        {
            get { return _CPU; }
            set { _CPU = value; }
        }

        [System.ComponentModel.ReadOnly(true)]
        public string OS
        {
            get { return _OS; }
            set { _OS = value; }
        }

        public string ToPackagedHWKey()
        {
            string returnv = "";
            XmlSerializer s = new XmlSerializer(typeof(HardwareKeyData));
            MemoryStream ms = new MemoryStream();
            s.Serialize(ms, this);
            byte[] bytes = ms.ToArray();
            foreach (byte i in bytes)
            {
                returnv += i.ToString() + ",";
            }
            returnv = returnv.Remove(returnv.Length - 1, 1);
            return returnv;
        }

        public static HardwareKeyData GetHardwareKeyDatafromPackagedHWKey(string phwk)
        {
            string[] bytesinstring = phwk.Split(',');
            List<byte> bytes = new List<byte>();
            foreach (string i in bytesinstring)
            {
                bytes.Add(Convert.ToByte(i));
            }
            MemoryStream ms = new MemoryStream(bytes.ToArray());
            ms.Position = 0;
            XmlSerializer s = new XmlSerializer(typeof(HardwareKeyData));
            return (HardwareKeyData)s.Deserialize(ms);
        }
    }
}
